{% extends 'custom_admin/base.html' %}

{% block content %}
<div class="row">
    <div class="col-md-2 mb-4">
        <div class="card text-center h-100">
            <div class="card-body">
                <i class="fas fa-box fa-3x text-primary mb-3"></i>
                <h3 class="text-primary">{{ product_count }}</h3>
                <p class="card-text">电商产品</p>
                <a href="{% url 'custom_admin_products' %}" class="btn btn-outline-primary btn-sm">管理产品</a>
            </div>
        </div>
    </div>
    
    <div class="col-md-2 mb-4">
        <div class="card text-center h-100">
            <div class="card-body">
                <i class="fas fa-comments fa-3x text-success mb-3"></i>
                <h3 class="text-success">{{ comment_count }}</h3>
                <p class="card-text">用户评论</p>
                <a href="{% url 'custom_admin_comments' %}" class="btn btn-outline-success btn-sm">管理评论</a>
            </div>
        </div>
    </div>
    
    <div class="col-md-2 mb-4">
        <div class="card text-center h-100">
            <div class="card-body">
                <i class="fas fa-star fa-3x text-warning mb-3"></i>
                <h3 class="text-warning">{{ rate_count }}</h3>
                <p class="card-text">用户评分</p>
                <a href="{% url 'custom_admin_rates' %}" class="btn btn-outline-warning btn-sm">管理评分</a>
            </div>
        </div>
    </div>
    
    <div class="col-md-2 mb-4">
        <div class="card text-center h-100">
            <div class="card-body">
                <i class="fas fa-tags fa-3x text-info mb-3"></i>
                <h3 class="text-info">{{ tag_count }}</h3>
                <p class="card-text">产品标签</p>
                <a href="{% url 'custom_admin_tags' %}" class="btn btn-outline-info btn-sm">管理标签</a>
            </div>
        </div>
    </div>
    
    <div class="col-md-2 mb-4">
        <div class="card text-center h-100">
            <div class="card-body">
                <i class="fas fa-users fa-3x text-danger mb-3"></i>
                <h3 class="text-danger">{{ user_count }}</h3>
                <p class="card-text">前台用户</p>
                <a href="{% url 'custom_admin_users' %}" class="btn btn-outline-danger btn-sm">管理用户</a>
            </div>
        </div>
    </div>
    
    <div class="col-md-2 mb-4">
        <div class="card text-center h-100">
            <div class="card-body">
                <i class="fas fa-database fa-3x text-secondary mb-3"></i>
                <h3 class="text-secondary">OpenGauss</h3>
                <p class="card-text">数据库</p>
                <span class="badge bg-success">连接正常</span>
            </div>
        </div>
    </div>
</div>

<!-- 操作历史记录部分 -->
<div class="row mt-4">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    <i class="fas fa-history me-2"></i>最近操作记录
                </h5>
                <span class="badge bg-primary">{{ total_logs }} 条记录</span>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th width="20%">时间</th>
                                <th width="15%">操作员</th>
                                <th width="15%">操作类型</th>
                                <th width="20%">对象类型</th>
                                <th width="30%">操作对象</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for log in recent_logs %}
                            <tr>
                                <td>
                                    <small class="text-muted">
                                        {{ log.action_time|date:"m-d H:i" }}
                                    </small>
                                </td>
                                <td>
                                    <span class="badge bg-secondary">{{ log.user.username }}</span>
                                </td>
                                <td>
                                    {% if log.action_flag == 1 %}
                                        <span class="badge bg-success">
                                            <i class="fas fa-plus me-1"></i>新增
                                        </span>
                                    {% elif log.action_flag == 2 %}
                                        <span class="badge bg-warning">
                                            <i class="fas fa-edit me-1"></i>修改
                                        </span>
                                    {% elif log.action_flag == 3 %}
                                        <span class="badge bg-danger">
                                            <i class="fas fa-trash me-1"></i>删除
                                        </span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if log.content_type %}
                                        <span class="badge bg-info">{{ log.content_type.name }}</span>
                                    {% else %}
                                        <span class="text-muted">未知</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <span class="text-truncate d-inline-block" style="max-width: 200px;" 
                                          title="{{ log.object_repr }}">
                                        {{ log.object_repr|truncatechars:30 }}
                                    </span>
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="5" class="text-center py-3 text-muted">
                                    <i class="fas fa-inbox fa-2x mb-2 d-block"></i>
                                    暂无操作记录
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-chart-pie me-2"></i>操作统计
                </h5>
            </div>
            <div class="card-body">
                {% for stat in action_stats %}
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <div>
                        {% if stat.flag == 1 %}
                            <i class="fas fa-plus text-success me-2"></i>
                        {% elif stat.flag == 2 %}
                            <i class="fas fa-edit text-warning me-2"></i>
                        {% elif stat.flag == 3 %}
                            <i class="fas fa-trash text-danger me-2"></i>
                        {% endif %}
                        {{ stat.name }}操作
                    </div>
                    <span class="badge bg-primary">{{ stat.count }}</span>
                </div>
                {% endfor %}
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-users me-2"></i>活跃用户
                </h5>
            </div>
            <div class="card-body">
                {% for stat in user_stats %}
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div>
                        <i class="fas fa-user text-primary me-2"></i>
                        {{ stat.user__username }}
                    </div>
                    <span class="badge bg-secondary">{{ stat.count }} 次</span>
                </div>
                {% empty %}
                <div class="text-center text-muted">
                    <i class="fas fa-user-slash mb-2"></i>
                    <p class="mb-0">暂无操作记录</p>
                </div>
                {% endfor %}
            </div>
        </div>
    </div>
</div>
{% endblock %}